/*!
 * 下拉日期控件.
 * ecp.qzz.ui.drop.popfilter.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.pwindow","qzz.listview","qzz.button"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.drop");
	/**
	 * 下拉日期控件.
	 */
	qzz.ui.drop.popfilter = qzz.Class(qzz.ui.drop.popwindow, {
		_create:function(){		
		    this.getOption().width = this.getNumberValue(this.getOption().width, 230);
		    this.getOption().height = this.getNumberValue(this.getOption().height, 298);
		    this.base();		    
		    this.getOption().rowHeight = this.getNumberValue(this.getOption().rowHeight, 20);
	    },
	    _render:function() {
	    	this.base();
	    	var margin = 8;
	        var width = this.getOption().width - 2 * margin;
	        var rowHeight = this.getOption().rowHeight;
	        var sumHeight = 0;
	        var pxs = "px";
	        var table = this.createElement("table");
	        this.setStyle(table, "filterBox");
	        var mrow = table.insertRow(0).insertCell(0);
	        this.setStyle(mrow,"c_common_td");
	        //排序选项列表.
	        var menuTab = this.createElement("table");
	        menuTab.border = 0;
	        menuTab.cellPadding = 0;
	        menuTab.cellSpacing = 0;
	        menuTab.style.width = width + pxs
	        var _this = this;
	        var mc = 0;
	        if(this.getOption().sort !== false) {
		        //排序选择项事件
		        this._addFilterRow(menuTab, mc, this.getI18n("qzz.sort_up", "升序"), rowHeight, function() {	        	
		        	_this.hide();
		        	_this.trigger("onSort",[_this._filterName, "up"]);
		        });
		        mc++;
	            this._addFilterRow(menuTab, mc, this.getI18n("qzz.sort_down", "降序"), rowHeight, function() {
	            	_this.hide();
		        	_this.trigger("onSort",[_this._filterName, "down"]);
		        });
	            mc++;
	            this._addFilterRow(menuTab, mc, this.getI18n("qzz.sort_def", "默认排序"), rowHeight, function() {
	            	_this.hide();
		        	_this.trigger("onSort",[_this._filterName, "none"]);
		        });
	            mc++;
		    }
            this._addFilterRow(menuTab, mc, this.getI18n("qzz.clearFilter", "清除筛选"), rowHeight, function() {
            	_this.hide();
	        	_this.trigger("onClearFilter",[_this._filterName]);
	        });
            mc++;
	        sumHeight += rowHeight * mc;
	        mrow.appendChild(menuTab);
	        var spitLine = table.insertRow(1).insertCell(0);
	        this.setStyle(spitLine,"c_common_td");
	        this._setHtml(spitLine, "<hr class='"+this._cssPrefix+"lineColor' style='background-color:#dddddd;'>", false);
	        spitLine.style.height = "10" + pxs;
	        sumHeight += 10;
	        var fbox = table.insertRow(2).insertCell(0);
	        this.setStyle(fbox,"c_common_td");
	        //创建过滤窗口控件.
	        this.filterBox = this.createElement("input");
	        this.filterBox.style.width = width + pxs;
	        this.setStyle(this.filterBox, "searchInput");
	        var emptyWord = this.getI18n("qzz.inputKey", "输入关键字");
	       // var emptyColor = "#6d6d6d";
	        //this.filterBox.value = emptyWord;
	        //this.filterBox.style.color = emptyColor;
	        this.filterBox.setAttribute("placeholder", emptyWord);
	        //过滤窗口初始处理.
//	        this.filterBox.onfocus = function() {
//	        	if(this.value === emptyWord) {
//	        		this.value = "";
//	        		this.style.color = "#000000";
//	        	}
//	        }
//	        this.filterBox.onblur = function() {
//	        	if(this.value === "") {
//	        		this.value = emptyWord;
//	        		this.style.color = emptyColor;
//	        	}
//	        }
	        //执行过滤事件.
	        this.filterBox.onkeyup = function() {
	        	_this.filterListView.clearFilter();
	        	var fv = _this.trim(this.value);
	        	_this.filterListView.addFilter(fv);
	        }
	        fbox.appendChild(this.filterBox);
	        fbox.style.height = rowHeight + pxs;
	        sumHeight += rowHeight;
	        this._lbox = table.insertRow(3).insertCell(0);
	        this.setStyle(this._lbox, "c_common_td");
	        var boxHeight = this.getOption().height - sumHeight - 48 - 4;
	        this._lbox.style.height = boxHeight + pxs;
	        this._lbox.style.verticalAlign = "top";
	        //创建列表面板
	        this.filterListView = new qzz.ui.listview(undefined, 
	        	{"cssPrefix":this._cssPrefix,selectAll:true,"multiSelect":true,"search":false});
	        this.filterListView.setParent(this._lbox);
	        this.filterListView.setHeight(boxHeight, false);
	        this.filterListView.setWidth(width);
	        this.filterListView.setTop(1);
	        
	        var btnTd = table.insertRow(4).insertCell(0);
	        this.setStyle(btnTd, "c_common_td");
	        var btnRect = this.createElement("DIV");
	        btnTd.appendChild(btnRect);
	        btnRect.style.width = "100%";
	        btnRect.style.height = "100%";
	        btnRect.style.textAlign = "right"; 
	        btnRect.style.position = "relative";
	        
	        this.getContainer().appendChild(table);
	        //处理确定按钮及事件
	        var btnWidth = 73;
	        var btnHeight = 28;
	        this.filterFixBtn = new qzz.ui.button(
	        	{"top":6,"left":width - btnWidth,"width":btnWidth,"height":btnHeight,"caption":this.getI18n("qzz.ok", "确定"),"fontSize":12})
	        this.filterFixBtn.setParent(btnRect);
	        this.filterFixBtn.bind("onclick",function(){
	        	_this.hide();
	        	_this.trigger("onFilter",[_this._filterName, _this.filterListView.length(true), _this.filterListView.getSelect()]);     	
	        });
	        this.filterFixBtn.setFocus();
	        this.filterFixBtn.refresh();
	        //处理取消按钮及事件.
	        this.filterCanselBtn = new qzz.ui.button(
	        		{"top":6,"left":width - btnWidth * 2 - margin,"width":btnWidth,"height":btnHeight,"caption":this.getI18n("qzz.cancel", "取消"),"fontSize":12});
	        this.filterCanselBtn.setParent(btnRect);
	        this.filterCanselBtn.bind("onclick",function(){ 
	        	_this.hide();
	        	_this.trigger("onCancel", [_this._filterName]);
	        });
	    },
	    refresh:function() {
	    	var mc = 5;
	    	if(this.getOption().sort === false) {
	    		mc = mc - 3;
	    	}
	    	var boxHeight = this.getOption().height - this.getOption().rowHeight * mc - 62;
	    	this.filterListView.setHeight(boxHeight);
	    	this._lbox.style.height = boxHeight + "px";
	    	this.filterFixBtn.setTop();
	    },
	    setHeight:function(height) {
	    	this.base(height);
	    	if(typeof height == "number") {
	    	    this.getOption().height = height;
	    	    this.refresh();
	    	}	    	
	    },
	    /**
	     * 添加过滤行.
	     */
	    _addFilterRow:function(menuTab, index, caption, height, events) {
	    	var mrow = menuTab.insertRow(index);
	        //实际菜单项目.
//	    	var _this = this;
//	        mrow.onmouseover = function() {
//	         	_this.setStyle(this, "popMouseOver");
//	        };
//	        mrow.onmouseout = function() {
//	          	_this.setStyle(this, "popCaption");
//	        };
	        this.setStyle(mrow, "popCaption");
	        var mcell = mrow.insertCell(0);
	        this.setStyle(mcell, "c_common_td");
	        this._setHtml(mcell, caption);
	        mcell.onclick = events;
	        mcell.style.height = height + "px";
	    },
	    setFilterName:function(value) {
	    	this._filterName = value;
	    },
	    getFilterName:function(){
	    	return this._filterName;
	    },
	    clearFilter:function() {
			this.filterListView.clearFilter();
	    },
//	    doShow:function() {
//	    	//this.logInfo(this.getName() + "show================");
//	        alert("show");
//	    },
//	    doHide:function() {
//	    	//this.logInfo(this.getName() + "hide================");
//	        alert("hide");
//	    },
	    execute:function(top, left, filterList, selList) {
	    	var ls = [];
	    	if(this.isArray(filterList)) {	    		
	    		if(typeof filterList[0] == "string") {
		    		var tmpJs = {};		    		
		    		for(var key in filterList) {
	    				if(filterList[key] === "") {
	    					filterList[key] = "("+this.getI18n("qzz.empty", "空白")+")";
	    				}
	    				if(this.isEmpty(tmpJs[filterList[key]])) {
		    				ls.push({
		    					"id":parseInt(key, 10),
		    					"text":filterList[key]
		    				});
		    				tmpJs[filterList[key]] = 1;
	    				}		    				
	    			}	
	    		} else {
	    			ls = filterList;
	    		}	    	    
	    	} else if(typeof filterList == "object") {
				if(this.isNotEmpty(filterList)) {
					var tmpJs = {};
					for(var key in filterList) {
						if(filterList[key] === "") {
							filterList[key] = "("+this.getI18n("qzz.empty", "空白")+")";
						}
						if(this.isEmpty(tmpJs[filterList[key]])) {
							ls.push({
								"id":key,
								"text":filterList[key]
							});
							tmpJs[filterList[key]] = 1;
						}		    				
					}
					tmpJs = null;
				}
	    	}
	    	this.filterListView.setItemList(ls);
	    	if(this.isArray(selList)) {
	    		this.filterListView.setSelectByText(selList);
	    	} else if(typeof selList == "object") {
	    		var vals = [];
	    		if(this.isNotEmpty(selList)) {    			
	    			for(var key in selList) {
	    				vals.push(key);
	    			}
	    		}
	    		if(vals.length > 0 || this.getOption().defSelectAll !== true) {
	    		    this.filterListView.setSelectByText(vals);
	    		} else {
	    			this.filterListView.doSelectAll();
	    		}
	    	}
	    	this.base(top, left);
	    	this.filterBox.focus();
	    	//F1217287 筛选窗口过滤框清除问题。
	    	this.filterBox.value = '';
	    	
	    }
	});
	_bindRenderFunc("qzzpopfilter", qzz.ui.drop.popfilter);
	return qzz.ui.drop.popfilter;
});
	